package com.techsen.tsweb.sys.security;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import javax.annotation.Resource;

import org.apache.shiro.authz.Permission;
import org.apache.shiro.authz.permission.RolePermissionResolver;
import org.springframework.stereotype.Component;

import com.techsen.tsweb.sys.domain.Acl;
import com.techsen.tsweb.sys.domain.Role;
import com.techsen.tsweb.sys.service.AclService;

@Component("aclRolePermissionResolver")
public class AclRolePermissionResolver implements RolePermissionResolver {

    @Resource
    private AclService aclService;

    /**
     * 根据角色ID获取权限集合
     */
    @Override
    public Collection<Permission> resolvePermissionsInRole(String roleId) {
        List<Permission> perms = new ArrayList<Permission>();
        List<Acl> acls = this.aclService.getListByEntity(new Acl()
                .setPrincipalType(Role.class.getSimpleName().toLowerCase())
                .setPrincipalId(roleId));
        for (Acl acl : acls) {
            perms.add(acl.getAclPermission());
        }
        return perms;
    }

}
